﻿#region System
using System;
using System.Collections.Generic;
using CF.Framework.Engine;
using CF.Framework.Engine.Controls;
using CF.Framework.Engine.Drawing;
using CF.Framework.Engine.Effects;
using CF.Framework.Engine.Misc;
using CF.Framework.Engine.Screens;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#endregion

namespace PSR.App.Screens
{
	public class WelcomeScreen : GameScreen
	{
		private AudioObject bgm;
		private AudioObject pressSound;

		private Background background;
		private TextureBox title;
		private TextureBox mask;
		private Label start;
		private Label copyright;

		private FadeInEffect backgroundEffect;
		private FadeInEffect titleEffect;
		private FadeInEffect maskEffect;
		private ScaleEffect startScaleEffect;
		private FlashEffect startFlashEffect;

		public WelcomeScreen()
		{
		}

		protected override void OnInitialize()
		{
			base.OnInitialize();

			this.Exiting += new EventHandler(LogoScreen_Exiting);
		}

		void LogoScreen_Exiting(object sender, EventArgs e)
		{
		}

		protected override void OnLoadContent()
		{
			base.OnLoadContent();

			bgm = this.AudioManager.Load<Song>(Metadata.Music.BGM01);
			pressSound = this.AudioManager.Load<SoundEffect>(Metadata.Sound.Grass);

			backgroundEffect = new FadeInEffect() { Duration = TimeSpan.FromMilliseconds(1000) };
			backgroundEffect.EffectEnd += new EventHandler(backgroundEffect_EffectEnd);

			background = new Background() { Context = Load<Texture2D>("Images/WelcomeBackground"), Opacity = 0, OpacityMask = DrawingUtility.CreateRectangleTexture(ScreenManager.GraphicsDevice, ScreenManager.ScreenWidth, ScreenManager.ScreenHeight, Color.Black) };
			background.Effects.Add(backgroundEffect);

			titleEffect = new FadeInEffect() { Duration = TimeSpan.FromMilliseconds(2000) };
			titleEffect.EffectEnd += new EventHandler(titleEffect_EffectEnd);

			title = new TextureBox() { Context = Load<Texture2D>("Images/WelcomeTitle"), Opacity = 0, Visibility = Visibility.Collapsed };
			title.Position = new Vector2(ScreenManager.MeasureCenter(new Vector2(title.Width, title.Height)).X, 30);
			title.Effects.Add(titleEffect);

			startScaleEffect = new ScaleEffect() { Duration = TimeSpan.FromMilliseconds(500), From = 1, To = 1.2f, IsLoop = true };
			startFlashEffect = new FlashEffect() { Duration = TimeSpan.FromMilliseconds(800), Interval = TimeSpan.FromMilliseconds(50) };
			startFlashEffect.EffectStart += new EventHandler(startFlashEffect_EffectStart);
			startFlashEffect.EffectEnd += new EventHandler(startFlashEffect_EffectEnd);

			start = new Label() { Text = GameResources.GetDescription("Welcome.Start"), Font = ScreenManager.SpriteFonts.LargeFont, BorderStyle = BorderStyle.None, Visibility = Visibility.Collapsed };
			start.Position = new Vector2(ScreenManager.MeasureCenter(new Vector2(start.Width, start.Height)).X, ScreenManager.ScreenHeight - 70 - start.Height);
			start.Effects.Add(startScaleEffect);
			start.Effects.Add(startFlashEffect);

			copyright = new Label() { Text = GameResources.GetDescription("Welcome.Copyright"), Font = ScreenManager.SpriteFonts.SmallFont, BorderStyle = BorderStyle.None, Visibility = Visibility.Collapsed };
			copyright.Position = new Vector2(ScreenManager.MeasureCenter(new Vector2(copyright.Width, copyright.Height)).X, ScreenManager.ScreenHeight - 10 - copyright.Height);

			maskEffect = new FadeInEffect() { Duration = TimeSpan.FromMilliseconds(500) };
			maskEffect.EffectEnd += new EventHandler(maskEffect_EffectEnd);

			mask = new TextureBox() { Context = DrawingUtility.CreateRectangleTexture(ScreenManager.GraphicsDevice, ScreenManager.ScreenWidth, ScreenManager.ScreenWidth, Color.Black), Visibility = Visibility.Collapsed };
			mask.Effects.Add(maskEffect);

			this.Components.Add(background);
			this.Components.Add(title);
			this.Components.Add(start);
			this.Components.Add(copyright);
			this.Components.Add(mask);
		}

		protected override void OnUnloadContent()
		{
			base.OnUnloadContent();
		}

		protected override void OnReady()
		{
			base.OnReady();

			backgroundEffect.Start();
		}

		protected override void OnHandleInput(InputState input)
		{
			base.OnHandleInput(input);

			if (maskEffect.IsRunning)
			{
				return;
			}

			if (input.IsPauseGame())
			{
				GameApp.Current.Exit();
				return;
			}

			if (titleEffect.IsCompleted
				&& startScaleEffect.IsRunning
				&& input.IsNewKeyPress())
			{
				startScaleEffect.Stop();
				startFlashEffect.Start();

				HandleInput = false;
			}
		}

		void backgroundEffect_EffectEnd(object sender, EventArgs e)
		{
			bgm.Play();

			titleEffect.Start();
		}

		void titleEffect_EffectEnd(object sender, EventArgs e)
		{
			start.Visibility = Visibility.Visible;
			copyright.Visibility = Visibility.Visible;

			startScaleEffect.Start();
		}

		void startFlashEffect_EffectStart(object sender, EventArgs e)
		{
			pressSound.Play();
		}

		void startFlashEffect_EffectEnd(object sender, EventArgs e)
		{
			maskEffect.Start();
		}

		void maskEffect_EffectEnd(object sender, EventArgs e)
		{
			bgm.Stop();

			ScreenController.GotoMainMenuScreen();
		}
	}
}
